#coding:utf-8
import os, sys
sys.path.append(".")

import pygtrie 
from jieba_wrapper import cutwords
from jieba_wrapper import biaodian 
from utils import *

intro_stopwords = {}
intro_stopwords['饰']=1
intro_stopwords['由']=1
intro_stopwords['该剧']=1
intro_stopwords['全剧']=1
intro_stopwords['本剧']=1
intro_stopwords['本片']=1
intro_stopwords['简介']=1
intro_stopwords['介绍']=1
intro_stopwords['剧情']=1
intro_stopwords['梗概']=1
intro_stopwords['的']=1
intro_stopwords['了']=1
intro_stopwords['讲述']=1
intro_stopwords['主要']=1
intro_stopwords['详情']=1
intro_stopwords['1']=1
intro_stopwords['2']=1
intro_stopwords['3']=1
intro_stopwords['4']=1
intro_stopwords['5']=1
intro_stopwords['6']=1
intro_stopwords['是']=1
intro_stopwords['也']=1
intro_stopwords['了']=1
intro_stopwords['这']=1
intro_stopwords['又']=1
intro_stopwords['却']=1
intro_stopwords['还是']=1
intro_stopwords['于是']=1
intro_stopwords['就是']=1
intro_stopwords['可是']=1
intro_stopwords['不']=1
intro_stopwords['那']=1
intro_stopwords['而']=1
intro_stopwords['才']=1
intro_stopwords['和']=1
intro_stopwords['但']=1
intro_stopwords['仅仅']=1
intro_stopwords['仅']=1
intro_stopwords['还']=1
intro_stopwords['以']=1
intro_stopwords['在']=1
intro_stopwords['只']=1
intro_stopwords[';']=1
intro_stopwords['.']=1
intro_stopwords['"']=1
intro_stopwords["'"]=1
intro_stopwords[',']=1
intro_stopwords['!']=1
intro_stopwords['~']=1
intro_stopwords['｀']=1
intro_stopwords['#']=1
intro_stopwords['$']=1
intro_stopwords['%']=1
intro_stopwords['&']=1
intro_stopwords['^']=1
intro_stopwords['(']=1
intro_stopwords[')']=1
intro_stopwords['=']=1
intro_stopwords['-']=1
intro_stopwords['_']=1
intro_stopwords['|']=1
intro_stopwords['<']=1
intro_stopwords['>']=1
intro_stopwords['?']=1
intro_stopwords['/']=1
intro_stopwords['}']=1
intro_stopwords['{']=1
intro_stopwords['[']=1
intro_stopwords[']']=1
intro_stopwords['【']=1
intro_stopwords['】']=1
intro_stopwords['｛']=1
intro_stopwords['｝']=1
intro_stopwords['（']=1
intro_stopwords['）']=1
intro_stopwords['＆']=1
intro_stopwords['。']=1
intro_stopwords['？']=1
intro_stopwords['！']=1
intro_stopwords['；']=1
intro_stopwords['，']=1
intro_stopwords['《']=1
intro_stopwords['》']=1
intro_stopwords['｜']=1
intro_stopwords['——']=1
intro_stopwords['...']=1


def extact_intro_names(intro, actors ):
    if not intro:
        return {}
    if not actors:
        return {}

    if isinstance(intro, str):
        intro = intro.decode('utf-8', 'ignore')

    num = len(intro)
    if num <= 30:
        return {}

    if not actors:
        actors = u''

    if isinstance(actors, str):
        actors = actors.decode('utf-8', 'ignore')

    actors = actors.replace(u'|',';').replace(u',', ';').split(u';')
    allnames = actors

    namenum = 0
    pname_dic = pygtrie.CharTrie()
    for pname in allnames:
        if not pname: continue
        if pname.isdigit(): continue
        pname_dic[ pname ] = 1
        namenum += 1
    if namenum <= 0:
        return {}

    #人物
    findnames = {}
    i = 0   
    if num >= 100:
        num = 100
    while i < num :
        res, val = pname_dic.longest_prefix( intro[i:])
        if res is None:
            i += 1
            continue
        i += len(res) 
        findnames[ res.encode('utf-8', 'ignore') ] = 1

    return findnames

def filt_words( words ):
    hashwords = {}
    if words is None:
        return 0, hashwords
    wordnum = 0
    for word in words:
        w = word['word'].strip()
        if w =="": continue
        flag = word['flag']
        if flag in 'eouxwp': 
            continue
        if w in intro_stopwords: 
            continue
        wordnum += 1
        if wordnum > 180: continue
        wgt = 0.8
        if flag.startswith('n') or flag.startswith('v'):
            wgt = 1.0
        if w in hashwords:
            hashwords[w] += wgt
        else:
            hashwords[w] = wgt

    return wordnum, hashwords


def score_intro( album, video, extra1 = '', extra2 = '' ):
    if not album or not video :
        return None

    if not isinstance(album, list):
        return None
    
    if not isinstance(video, list):
        return None

    allnum1, hash1 = filt_words( album )
    allnum2, hash2 = filt_words( video )

    if allnum1 <= 5:
        return None

    if allnum2 <= 5:
        return None

    _exhash1,_exhash2 = {}, {}
    if extra1:
        exwords = cutwords( extra1 )
        _, _exhash1  = filt_words( exwords )
    if extra2:
        exwords = cutwords( extra2 )
        _, _exhash2 = filt_words( exwords )

    num1, num2 = 0, 0
    sum1, sum2 = 0, 0
    for word in hash1:
        wgt = hash1[word]
        sum1 += wgt
        if word in hash2:
            num1 += wgt
        elif word in _exhash2:
            num1 += wgt

    for word in hash2:
        wgt = hash2[word]
        sum2 += wgt
        if word in hash1:
            num2 += wgt
        elif word in _exhash1:
            num2 += wgt

    if abs(num1 - sum1) <= 0.001 and len(hash1) >= 35:
        return 1.0
    if abs(num2 - sum2) <= 0.001 and len(hash2) >= 35:
        return 1.0

    sim = (num1+num2+0.0) / (sum1+sum2 )    

    if sum1 * 4 <= sum2 :
        sim *= 1.1
    elif sum2 * 4 <=  sum1: 
        sim *= 1.1

    return sim

def score_intro_old( album, video, extra1 = '', extra2 = '' ):
    if not album or not video :
        return None

    if isinstance(album, unicode):
        album = album.encode('utf-8', 'ignore')
    
    if isinstance(video, unicode):
        video = video.encode('utf-8', 'ignore')

    if not isinstance(album, str):
        return None

    if not isinstance(video, str):
        return None

    if len(album) <= 50:
        return None

    if len(video) <= 50:
        return None

    words1 = cutwords( album )
    words2 = cutwords( video )
    sim = score_intro( words1, words2, extra1, extra2 )
    return sim

def test():
    f = open('bin/testcase/intro.txt', 'rb')
    for line in f:
        line = line.strip()
        fields = line.split('\t')
        lines = [ line.strip() for line in fields if line.strip() != "" ]
        if len(lines) == 2:
            print '###',lines[0]
            print '###',lines[1]
            print score_intro_old( lines[1], lines[0])
            print 
            print 
            pass
        elif len(lines) == 1:
            print '###',lines[0]
            print 
            print score_intro_old( '', lines[0])
            print 
            print 
            pass
    f.close()
    print 
    #print 'None,None',score_intro( None, '')
    x = '张国民;张凯丽;王平'
    y = '张国民;张凯丽;凌慧'
    line1 = """南方某大城市，在一幢公寓里，住着两个大龄青年：康凯住19层，是健美中心减肥俱乐部的营养师；韩佩佩住21层，是美容厅的美容师。他们在各自的工作岗位上成绩卓著，但也由于职业的限制，迟迟没有找到意中人。两人近在咫尺，却并不相识。一天，康凯在电梯里捂以韩佩佩遗落的手帕，里面还包着一只钻石发卡，他好心好意追到车站想还给她，被车上的人斥为神经病，气得他把手帕扔在地上，又被卫生监督员当作乱扔罚款，他只得自叹倒霉。康凯的朋友燕谷觉得这是帮康凯结识这位姑娘的好机会，便悄悄捡起康凯扔进垃圾箱的发卡，决定自己送上楼去，替康凯牵线搭桥。韩佩佩的朋友沈巴黎也劝佩佩自己上门取回发卡，用意也是想让结识一个新朋友。一系列误会就此产生。燕谷上楼见到沈巴黎，误以为她就是韩佩佩；韩佩佩来到康凯要回发卡，康凯遍寻不见，佩佩以为受了捉弄，率门而去。燕谷回来称赞佩佩（沈鍪）漂亮热情，拿出又带回来的发卡让康凯主动寻机追求。	"""
    line2 = """简介：南方某大城市的一栋公寓里，住着两个大龄青年：19层的康凯（张国民 饰）和21层的韩佩佩（张凯丽 饰）。两人近在咫尺，却不相识。一个偶然的机会，康凯捡到了佩佩遗落的钻石发卡。康凯的朋友和韩佩佩的朋友觉得这是撮合两人的好机会，可是却闹出很多误会，结果两人不欢而散。于是两个好心的朋友又分别帮他俩登了征婚启示。从此，康凯和佩佩便开始通信交往，信中不报姓名，只冠“某男”、“某女”。结果，两人约见的时候又闹出误会。后来才发现对方就是自己的意中人。康凯就是“某男”，佩佩就是“某女”。"""
    #x , y = '', ''
    print score_intro_old(line, x, y)
    pass


if __name__ == "__main__":
    test()
    sys.exit()
    x = '张艺谋'
    y = '张国民;张凯丽;王平'
    line = u"""简介：南方某大城市的一栋公寓里，住着两个大龄青年：19层的康凯（张国民饰）和21层的韩佩佩（张凯丽 饰）。两人近在咫尺，却不相识。一个偶然的机会，康凯捡到了佩佩遗落的钻石发卡。康凯的朋友和韩佩佩的朋友觉得这是撮合两人的好机会，可是却闹出很多误会，结果两人不欢而散。于是两个好心的朋友又分别帮他俩登了征婚启示。从此，康凯和佩佩便开始通信交往，信中不报姓名，只冠“某男”、“某女”。结果，两人约见的时候又闹出误会。后来才发现对方就是自己的意中人。康凯就是“某男”，佩佩就是“某女”。"""
    d = extact_intro_names( line, y)
    for name in d:
        print name
    print '======================================'
    x = None
    y = u'张国民;张凯丽;王平'
    d = extact_intro_names( line,y)
    for name in d:
        print name
    print '======================================'

    x = "albs "
    y = u'张国民;张凯丽;王平'
    d = extact_intro_names( line, y)
    for name in d:
        print name
    print '======================================'

    x = ""
    y = u'张国民 ,张凯丽;王平'
    d = extact_intro_names( line, y)
    for name in d:
        print name
    print '======================================'

    x = 7
    y = u'张国民 ,张凯丽;王平'
    d = extact_intro_names( line,y)
    for name in d:
        print name
    print '======================================'

    

